package com.sjqi.demo;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;

/**
 * @author qishaojun
 */
public class TestSpringV1 {
    private Map<String, Object> singletonObjects = new HashMap<>();
    private Map<String, BeanDefinition> beanDefinitionMap = new HashMap<>();

    public Object getBean(String beanName) {
        Object singletonObject = this.singletonObjects.get(beanName);
        if (null != singletonObject) {
            return singletonObject;
        }
        BeanDefinition beanDefinition = this.beanDefinitionMap.get(beanName);
//        String scope = "";
//        if ("singleton".equals(scope)) {
//
//        } else if ("prototype".equals(scope)) {
//
//        }
        //谁管数据 谁提供数据的基本操作
        if (beanDefinition.isSingleton()) {
            singletonObject = doCreateBean();
            this.singletonObjects.put(beanName, singletonObject);
        } else {
            singletonObject = doCreateBean();

        }
        return singletonObject;
    }

    private Object doCreateBean(BeanDefinition beanDefinition) {
        Object bean = createBeanByConstructor(beanDefinition);
        return bean;
    }

    private Object createBeanByConstructor(BeanDefinition beanDefinition) {
        // 静态工厂工厂实例
        //TODO:反射创建
        Class clazzType = beanDefinition.getClazzType();
        Constructor constructor = null;
        try {
            constructor = clazzType.getDeclaredConstructor();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return null;//constructor.newInstance();
    }

    private void populateBean(BeanDefinition beanDefinition) {
        //遍历PropertyValue
        //for
        //resolveValue
        //setProperty
    }

    private void setProperty() {

    }

    private Object resolveValue(Object obj) {
        //读取 TypedStringValue  typedStringValue: 封装了String 的value 和类型
        //策略模式判断类型 转型
        //赋值
        return null;
    }

    private void initializeBean(Object obj, BeanDefinition beanDefinition) {
        //Aware
        //initilizingBean
        invokeInitMethod(obj, beanDefinition);
    }

    private void invokeInitMethod(Object obj, BeanDefinition beanDefinition) {
        //反射调用 init 方法
        //有就调用 没有就不调用
    }
}
